package ru.isys.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

import ru.isys.activity.MainActivity;
import ru.isys.xml.XmlItem;

public class BindPanel extends JPanel implements HaveMainListener {

	List<BindItem> elements;
	GridBagConstraints constraints;
	GridBagLayout gridbag;
	JButton ok;
	private MainActivity listener;

	public BindPanel(final MainActivity listener) {
		gridbag = new GridBagLayout();
		constraints = new GridBagConstraints();
		setLayout(gridbag);
		ok = new JButton("Хоккей");
		ok.addActionListener(new ActionListener() {
			 
            public void actionPerformed(ActionEvent e)
            {
                
            	listener.onSaveBind();
            }
        });
		setListener(listener);
	}

	public void setItems(List<BindItem> elements) {
		this.removeAll();
		this.elements = elements;
		for (BindItem currentItem : elements) {
			JPanel wrap = new JPanel();
			wrap.add(currentItem);
			constraints.insets = new Insets(10, currentItem.getLevel() * 80, 10, 0);
			constraints.gridx = 0;
			gridbag.setConstraints(wrap, constraints);
			add(wrap);
		}
		constraints.insets = new Insets(10, 10, 10, 0);
		constraints.gridx = 0;
		gridbag.setConstraints(ok, constraints);
		add(ok);
		validate();
	}

	public void setData(XmlItem data) {
		setItems(elements);
	}

	@Override
	public void setListener(MainActivity listener) {
		this.listener = listener;
	}
	
	public List<BindItem> getBindItems(){
	    return elements;
	}

}
